﻿namespace Edge.Utilities
{
    using System;
    using System.Linq;

    /// <summary>
    /// Contains various array-related tools and utilities.
    /// </summary>
    public static class ArrayUtilities
    {
        /// <summary>
        /// Combines a series of arrays of the same type into a single array.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of the array.</typeparam>
        /// <param name="arrays">The arrays to combine.</param>
        /// <returns>A single array of type T, with all of the contents of the arrays.</returns>
        /// <example>
        ///     <code language="c#">
        ///         int[] array1 = new int[] { 1, 2, 3 };
        ///         int[] array2 = new int[] { 4, 5, 6 };
        ///         int[] combined = ArrayUtilities.Combine<int>(array1, array2); // results in: int[] { 1, 2, 3, 4, 5, 6 }
        ///     </code>
        /// </example>
        public static T[] Combine<T>(params T[][] arrays)
        {
            T[] rv = new T[arrays.Sum(a => a.Length)];

            int offset = 0;

            foreach (T[] array in arrays)
            {
                Array.Copy(array, 0, rv, offset, array.Length);

                offset += array.Length;
            }

            return rv;
        }
    }
}
